//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ConvertibleFloatingRateBond.h"
using namespace Cephei::QL::Experimental;
#include <gen/QL/Exercise.h>
#include <gen/QL/Cashflows/Dividend.h>
#include <gen/QL/Instruments/Callability.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Schedule.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/CashFlow.h>
#include <gen/QL/PricingEngine.h>
#include <gen/QL/Experimental/ConvertibleBond.h>
using namespace Cephei::QL;
using namespace Cephei::QL::Cashflows;
using namespace Cephei::QL::Instruments;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL::Times;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::CConvertibleFloatingRateBond::CConvertibleFloatingRateBond (Cephei::QL::IExercise^ exercise, Double conversionRatio, Cephei::Core::IVector<Cephei::QL::Cashflows::IDividend^>^ dividends, Cephei::Core::IVector<Cephei::QL::Instruments::ICallability^>^ callability, Cephei::QL::IQuote^ creditSpread, DateTime issueDate, UInt32 settlementDays, Cephei::QL::Indexes::IIborIndex^ index, UInt32 fixingDays, Cephei::Core::IVector<Double>^ spreads, Cephei::QL::Times::IDayCounter^ dayCounter, Cephei::QL::Times::ISchedule^ schedule, Microsoft::FSharp::Core::FSharpOption<Double>^ redemption, Cephei::QL::IPricingEngine^ QL_Pricer) : CConvertibleBond(CConvertibleFloatingRateBond::typeid)
{
    CExercise^ _Cexercise;
    CQuote^ _CcreditSpread;
    CIborIndex^ _Cindex;
    CDayCounter^ _CdayCounter;
    CSchedule^ _Cschedule;
    try
    {
#ifdef HANDLE
        _phConvertibleFloatingRateBond = NULL;
#endif
        _Cexercise = safe_cast<CExercise^> (exercise);
        _Cexercise->Lock();
        boost::shared_ptr<QuantLib::Exercise>& _exercise = static_cast<boost::shared_ptr<QuantLib::Exercise>&> (_Cexercise->GetShared ()); 
        QuantLib::Real _conversionRatio = (QuantLib::Real)ValueHelper::Convert (conversionRatio); //d
        dividends ->Lock ();
        INativeVector<Cephei::QL::Cashflows::IDividend^>^ _NCIdividends = dividends->getFeature (NativeFeature::shared_ptr);
        CDividendVector^ _NCdividends = safe_cast<CDividendVector^>(_NCIdividends);
        std::vector<boost::shared_ptr<QuantLib::Dividend> >& _dividends = static_cast<std::vector<boost::shared_ptr<QuantLib::Dividend> >&> (_NCdividends->GetShared ());
        callability ->Lock ();
        INativeVector<Cephei::QL::Instruments::ICallability^>^ _NCIcallability = callability->getFeature (NativeFeature::shared_ptr);
        CCallabilityVector^ _NCcallability = safe_cast<CCallabilityVector^>(_NCIcallability);
        std::vector<boost::shared_ptr<QuantLib::Callability> >& _callability = static_cast<std::vector<boost::shared_ptr<QuantLib::Callability> >&> (_NCcallability->GetShared ());
        _CcreditSpread = safe_cast<CQuote^> (creditSpread);
        _CcreditSpread->Lock();
        Handle<QuantLib::Quote>& _creditSpread = static_cast<Handle<QuantLib::Quote>&> (_CcreditSpread->GetHandle ()); 
        QuantLib::Date _issueDate = (QuantLib::Date)ValueHelper::Convert (issueDate); //d
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        _Cindex = safe_cast<CIborIndex^> (index);
        _Cindex->Lock();
        boost::shared_ptr<QuantLib::IborIndex>& _index = static_cast<boost::shared_ptr<QuantLib::IborIndex>&> (_Cindex->GetShared ()); 
        QuantLib::Natural _fixingDays = (QuantLib::Natural)ValueHelper::Convert (fixingDays); //d
        spreads->Lock();
        INativeVector<Double>^ _NCIspreads = spreads->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCspreads = safe_cast<CDoubleVector^>(_NCIspreads);
        std::vector<QuantLib::Spread>& _spreads = static_cast<std::vector<QuantLib::Spread>&> (_NCspreads->GetReference ());
        _CdayCounter = safe_cast<CDayCounter^> (dayCounter);
        _CdayCounter->Lock();
        QuantLib::DayCounter& _dayCounter = static_cast<QuantLib::DayCounter&> (_CdayCounter->GetReference ()); 
        _Cschedule = safe_cast<CSchedule^> (schedule);
        _Cschedule->Lock();
        QuantLib::Schedule& _schedule = static_cast<QuantLib::Schedule&> (_Cschedule->GetReference ()); 
        QuantLib::Real _redemption = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (redemption) ? (QuantLib::Real)ValueHelper::Convert (redemption->Value) : 100); //4
        _ppConvertibleFloatingRateBond = new boost::shared_ptr<QuantLib::ConvertibleFloatingRateBond> (new QuantLib::ConvertibleFloatingRateBond ( _exercise,  _conversionRatio,  _dividends,  _callability,  _creditSpread,  _issueDate,  _settlementDays,  _index,  _fixingDays,  _spreads,  _dayCounter,  _schedule,  _redemption ));
        CPricingEngine^ _CQL_Pricer = safe_cast<CPricingEngine^> (QL_Pricer);
        boost::shared_ptr<QuantLib::PricingEngine>& _QL_Pricer = static_cast<boost::shared_ptr<QuantLib::PricingEngine>&> (_CQL_Pricer->GetShared ());
        (*_ppConvertibleFloatingRateBond)->setPricingEngine (_QL_Pricer);
        SetConvertibleBond (boost::dynamic_pointer_cast<QuantLib::ConvertibleBond> (*_ppConvertibleFloatingRateBond));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cexercise != nullptr) _Cexercise->Unlock();
        if (dividends != nullptr) dividends->Unlock();    //not optional
        if (callability != nullptr) callability->Unlock();    //not optional
        if (_CcreditSpread != nullptr) _CcreditSpread->Unlock();
        if (_Cindex != nullptr) _Cindex->Unlock();
        if (spreads != nullptr) spreads->Unlock();    //not optional
        if (_CdayCounter != nullptr) _CdayCounter->Unlock();
        if (_Cschedule != nullptr) _Cschedule->Unlock();
    }
}
Cephei::QL::Experimental::CConvertibleFloatingRateBond::CConvertibleFloatingRateBond (boost::shared_ptr<QuantLib::ConvertibleFloatingRateBond>& childNative, Object^ owner) : CConvertibleBond(CConvertibleFloatingRateBond::typeid)
{
#ifdef HANDLE
	_phConvertibleFloatingRateBond = NULL;
#endif
	_ppConvertibleFloatingRateBond = &childNative;
    _ppConvertibleBond = new boost::shared_ptr<QuantLib::ConvertibleBond> (boost::dynamic_pointer_cast<QuantLib::ConvertibleBond> (*_ppConvertibleFloatingRateBond));
}
Cephei::QL::Experimental::CConvertibleFloatingRateBond::CConvertibleFloatingRateBond (QuantLib::ConvertibleFloatingRateBond& childNative, Object^ owner) : CConvertibleBond(CConvertibleFloatingRateBond::typeid)
{
#ifdef HANDLE
	_phConvertibleFloatingRateBond = NULL;
#endif
	_ppConvertibleFloatingRateBond = new boost::shared_ptr<QuantLib::ConvertibleFloatingRateBond> (&childNative);
    _ppConvertibleBond = new boost::shared_ptr<QuantLib::ConvertibleBond> (boost::dynamic_pointer_cast<QuantLib::ConvertibleBond> (*_ppConvertibleFloatingRateBond));
    _ConvertibleFloatingRateBondOwner = owner;
    _ConvertibleBondOwner = owner;
}

Cephei::QL::Experimental::CConvertibleFloatingRateBond::CConvertibleFloatingRateBond (CConvertibleFloatingRateBond^ copy) : CConvertibleBond(CConvertibleFloatingRateBond::typeid)
{
#ifdef HANDLE
	_phConvertibleFloatingRateBond = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppConvertibleFloatingRateBond = new boost::shared_ptr<QuantLib::ConvertibleFloatingRateBond> (copy->GetShared());
        _ppConvertibleBond = new boost::shared_ptr<QuantLib::ConvertibleBond> (boost::dynamic_pointer_cast<QuantLib::ConvertibleBond> (*_ppConvertibleFloatingRateBond));
    }
}
Cephei::QL::Experimental::CConvertibleFloatingRateBond::CConvertibleFloatingRateBond (PLATFORM::Type^ t) : CConvertibleBond(CConvertibleFloatingRateBond::typeid)
{
#ifdef HANDLE
	_phConvertibleFloatingRateBond = NULL;
#endif
	if (!t->IsSubclassOf(CConvertibleFloatingRateBond::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::CConvertibleFloatingRateBond::CConvertibleFloatingRateBond (QuantLib::Handle<QuantLib::ConvertibleFloatingRateBond>& childNative, Object^ owner)  : CConvertibleBond(CConvertibleFloatingRateBond::typeid)
{
	_phConvertibleFloatingRateBond = &childNative;
	_ppConvertibleFloatingRateBond = &static_cast<boost::shared_ptr<QuantLib::ConvertibleFloatingRateBond>>(childNative.currentLink());
    _ppConvertibleBond = new boost::shared_ptr<QuantLib::ConvertibleBond> (boost::dynamic_pointer_cast<QuantLib::ConvertibleBond> (*_ppConvertibleFloatingRateBond));
    _ConvertibleFloatingRateBondOwner = owner;
}
Cephei::QL::Experimental::CConvertibleFloatingRateBond::CConvertibleFloatingRateBond (QuantLib::Handle<QuantLib::ConvertibleFloatingRateBond> childNative)  : CConvertibleBond(CConvertibleFloatingRateBond::typeid)
{
	_phConvertibleFloatingRateBond = &childNative;
	_ppConvertibleFloatingRateBond = &static_cast<boost::shared_ptr<QuantLib::ConvertibleFloatingRateBond>>(childNative.currentLink());
    _ppConvertibleBond = new boost::shared_ptr<QuantLib::ConvertibleBond> (boost::dynamic_pointer_cast<QuantLib::ConvertibleBond> (*_ppConvertibleFloatingRateBond));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::CConvertibleFloatingRateBond::CConvertibleFloatingRateBond (QuantLib::ConvertibleFloatingRateBond childNative)  : CConvertibleBond(CConvertibleFloatingRateBond::typeid)
{
#ifdef HANDLE
	_phConvertibleFloatingRateBond = NULL;
#endif
	_ppConvertibleFloatingRateBond = new boost::shared_ptr<QuantLib::ConvertibleFloatingRateBond> (new QuantLib::ConvertibleFloatingRateBond (childNative));
    _ppConvertibleBond = new boost::shared_ptr<QuantLib::ConvertibleBond> (boost::dynamic_pointer_cast<QuantLib::ConvertibleBond> (*_ppConvertibleFloatingRateBond));
}
#endif

Cephei::QL::Experimental::CConvertibleFloatingRateBond::~CConvertibleFloatingRateBond ()
{
    if (_ppConvertibleFloatingRateBond != NULL)
    {
	    delete _ppConvertibleFloatingRateBond;
        _ppConvertibleFloatingRateBond = NULL;
    }
}
Cephei::QL::Experimental::CConvertibleFloatingRateBond::!CConvertibleFloatingRateBond ()
{
    if (_ppConvertibleFloatingRateBond != NULL)
    {
	    delete _ppConvertibleFloatingRateBond;
    }
}
QuantLib::ConvertibleFloatingRateBond& Cephei::QL::Experimental::CConvertibleFloatingRateBond::GetReference ()
{
    if (_ppConvertibleFloatingRateBond == NULL) throw REFNEW NativeNullException ();
	return **_ppConvertibleFloatingRateBond;
}
boost::shared_ptr<QuantLib::ConvertibleFloatingRateBond>& Cephei::QL::Experimental::CConvertibleFloatingRateBond::GetShared ()
{
    if (_ppConvertibleFloatingRateBond == NULL) throw REFNEW NativeNullException ();
	return *_ppConvertibleFloatingRateBond;
}
QuantLib::ConvertibleFloatingRateBond* Cephei::QL::Experimental::CConvertibleFloatingRateBond::GetPointer ()
{
    if (_ppConvertibleFloatingRateBond == NULL) throw REFNEW NativeNullException ();
	return &**_ppConvertibleFloatingRateBond;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ConvertibleFloatingRateBond>& Cephei::QL::Experimental::CConvertibleFloatingRateBond::GetHandle ()
{
	if (_phConvertibleFloatingRateBond == NULL)
	{
		_phConvertibleFloatingRateBond = new Handle<QuantLib::ConvertibleFloatingRateBond> (*_ppConvertibleFloatingRateBond);
	}
	return *_phConvertibleFloatingRateBond;
}
#endif
bool Cephei::QL::Experimental::CConvertibleFloatingRateBond::HasNative () 
{
	return (_ppConvertibleFloatingRateBond != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::IConvertibleFloatingRateBond^ Cephei::QL::Experimental::CConvertibleFloatingRateBond_Factory::Create (Cephei::QL::IExercise^ exercise, Double conversionRatio, Cephei::Core::IVector<Cephei::QL::Cashflows::IDividend^>^ dividends, Cephei::Core::IVector<Cephei::QL::Instruments::ICallability^>^ callability, Cephei::QL::IQuote^ creditSpread, DateTime issueDate, UInt32 settlementDays, Cephei::QL::Indexes::IIborIndex^ index, UInt32 fixingDays, Cephei::Core::IVector<Double>^ spreads, Cephei::QL::Times::IDayCounter^ dayCounter, Cephei::QL::Times::ISchedule^ schedule, Microsoft::FSharp::Core::FSharpOption<Double>^ redemption, Cephei::QL::IPricingEngine^ QL_Pricer)
{
    return REFNEW CConvertibleFloatingRateBond ( exercise,  conversionRatio,  dividends,  callability,  creditSpread,  issueDate,  settlementDays,  index,  fixingDays,  spreads,  dayCounter,  schedule,  redemption,  QL_Pricer);
}
